package com.es.disped.web.controller.coursetemplate;

import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.es.disped.api.coursetemplate.CourseTemplateService;
import com.es.disped.common.constant.RecordStatus;
import com.es.disped.common.model.MsgModel;
import com.es.disped.common.model.PageModel;
import com.es.disped.core.controller.BaseController;
import com.es.disped.dao.model.CourseForeignModel;
import com.es.disped.dao.model.CourseTemplate;
import com.es.disped.dao.model.CourseTemplateExample;
import com.es.disped.dao.model.LevelForeignModel;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;


/**
 * @author Anson<br>
 *	Copyright by EasyShare 2019<br>
 *  
 *  All right reserved<br>
 *
 *  Created on 2019年7月3日 上午9:52:12<br>
 *  名称：CourseTemplateController.java<br>
 *  描述：课程模板管理<br>
 */
@Api(value="课程模板管理")
@Controller
@RequestMapping("/manage/course/template")
public class CourseTemplateController extends BaseController {
	
	private final String COURSE_NAMESPACE="com.es.disped.dao.mapper.course.CourseCustomMapper";
	private final String LEVEL_NAMESPACE="com.es.disped.dao.mapper.level.LevelCustomMapper";
	private final String TEMPLATE_NAMESPACE="com.es.disped.dao.mapper.template.TemplateCustomMapper";

	@Autowired
	CourseTemplateService courseTemplateService;
	
	@ApiOperation(value="保存课程模板",response=MsgModel.class,httpMethod="POST",notes="保存课程模板")
	@RequiresPermissions(value={"admin:save","sys:save"},logical=Logical.OR)
	@Transactional
	@RequestMapping(value="/save",method=RequestMethod.POST)	
	public @ResponseBody MsgModel save(@RequestBody @ApiParam(name="courseTemplate",value="课程模板对象",required=true) CourseTemplate courseTemplate)
	{
		int count=0;
		if(Optional.of(courseTemplate)
				.filter(templateName->courseTemplate.getTemplateName()!=null)
				.filter(courseId->courseTemplate.getCourseId()!=null)
				.filter(levelId->courseTemplate.getLevelId()!=null)
				.filter(minAge->courseTemplate.getMinAge()!=null)
				.filter(maxAge->courseTemplate.getMaxAge()!=null)
				.filter(type->courseTemplate.getCourseType()!=null)
				.filter(book->courseTemplate.getCourseBook()!=null)
				.isPresent())
		{
			courseTemplate.setTemplateId(getUUID());
			CourseForeignModel courseForeignModel=this.getService().selectOne(
					COURSE_NAMESPACE+".getCourseForeignModel", courseTemplate.getCourseId());
			courseTemplate.setCourseName(courseForeignModel.getCourseName());
			LevelForeignModel levelForeignModel=this.getService().selectOne(
					LEVEL_NAMESPACE+".getLevelForeignModel",courseTemplate.getLevelId());
			courseTemplate.setLevelName(levelForeignModel.getLevelName());
			courseTemplate.setCourseValid(RecordStatus.USEAGE.getValue());
			count=courseTemplateService.insertSelective(courseTemplate);
		}
		return this.resultMsg(count==1?HttpStatus.OK.value():HttpStatus.EXPECTATION_FAILED.value(),
				count==1?"保存成功":"保存失败",count==1?courseTemplate:"");
	}
	
	@ApiOperation(value="更新课程模板",response=MsgModel.class,httpMethod="POST",notes="更新课程模板")
	@RequiresPermissions(value={"admin:update","sys:update"},logical=Logical.OR)
	@Transactional
	@RequestMapping(value="/update",method=RequestMethod.POST)
	public @ResponseBody MsgModel update(@RequestBody @ApiParam(name="courseTemplate",value="课程模板对象",required=true) CourseTemplate courseTemplate)
	{
		int count=0;
		if(!this.isNull(courseTemplate.getTemplateId()))
		{
			if(!this.isNull(courseTemplate.getCourseId()))
			{
				CourseForeignModel courseForeignModel=this.getService().selectOne(
						COURSE_NAMESPACE+".getCourseForeignModel", courseTemplate.getCourseId());
				courseTemplate.setCourseName(courseForeignModel.getCourseName());
			}
			if(!this.isNull(courseTemplate.getLevelId()))
			{
				LevelForeignModel levelForeignModel=this.getService().selectOne(
						LEVEL_NAMESPACE+".getLevelForeignModel",courseTemplate.getLevelId());
				courseTemplate.setLevelName(levelForeignModel.getLevelName());
			}
			count=courseTemplateService.updateByPrimaryKeySelective(courseTemplate);
		}
		return this.resultMsg(count==1?HttpStatus.OK.value():HttpStatus.EXPECTATION_FAILED.value(),
				count==1?"更新成功":"更新失败",count==1?courseTemplate:"");
	}
	
	@ApiOperation(value="删除课程模板",response=MsgModel.class,httpMethod="POST",notes="删除课程模板")
	@RequiresPermissions(value={"admin:delete","sys:delete"},logical=Logical.OR)
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	public @ResponseBody MsgModel delete(@RequestBody @ApiParam(name="courseTemplate",value="课程模板对象",required=true) CourseTemplate courseTemplate)
	{
		int count=0;
		if(!this.isNull(courseTemplate.getTemplateId()))
		{
			count=this.getService().delete(TEMPLATE_NAMESPACE+".deleteTemplateModel", courseTemplate.getTemplateId());
		}
		return this.resultMsg(count>=1?HttpStatus.OK.value():HttpStatus.EXPECTATION_FAILED.value(),
				count>=1?"删除成功":"删除失败",count>=1?courseTemplate:"");
	}
	
	
	@ApiOperation(value="检索课程模板",response=MsgModel.class,httpMethod="POST",notes="检索课程模板")
	@RequestMapping(value="/list",method=RequestMethod.POST)
	public @ResponseBody MsgModel list(@RequestBody @ApiParam(name="courseTemplate",value="课程模板对象",required=false) CourseTemplate courseTemplate)
	{
		CourseTemplateExample listExample=new CourseTemplateExample();
		CourseTemplateExample.Criteria criteria=listExample.createCriteria();
		if(!this.isNull(courseTemplate.getTemplateId()))
		{
			criteria.andTemplateIdEqualTo(courseTemplate.getTemplateId());
		}
		if(!this.isNull(courseTemplate.getTemplateName()))
		{
			criteria.andTemplateNameEqualTo(courseTemplate.getTemplateName());
		}
		if(!this.isNull(courseTemplate.getCourseId()))
		{
			criteria.andCourseIdEqualTo(courseTemplate.getCourseId());
		}
		if(!this.isNull(courseTemplate.getCourseName()))
		{
			criteria.andCourseNameLike("%"+courseTemplate.getCourseName()+"%");
		}
		if(!this.isNull(courseTemplate.getLevelId()))
		{
			criteria.andLevelIdEqualTo(courseTemplate.getLevelId());
		}
		if(!this.isNull(courseTemplate.getLevelName()))
		{
			criteria.andLevelNameLike("%"+courseTemplate.getLevelName()+"%");
		}
		if(!this.isNull(courseTemplate.getMinAge()))
		{
			criteria.andMinAgeGreaterThanOrEqualTo(courseTemplate.getMinAge());
		}
		if(!this.isNull(courseTemplate.getMaxAge()))
		{
			criteria.andMaxAgeLessThanOrEqualTo(courseTemplate.getMaxAge());
		}
		if(!this.isNull(courseTemplate.getCourseType()))
		{
			criteria.andCourseTypeLike("%"+courseTemplate.getCourseType()+"%");
		}
		List<CourseTemplate> courseTemplates=courseTemplateService.selectByExample(listExample);
		return this.resultMsg(!this.isNull(courseTemplates)?HttpStatus.OK.value():HttpStatus.EXPECTATION_FAILED.value(),
				!this.isNull(courseTemplates)?"课程模板加载成功":"课程模板加载失败",!this.isNull(courseTemplates)?courseTemplates:"");
	}
	
	
	@ApiOperation(value="课程模板分页加载",response=MsgModel.class,httpMethod="POST",notes="课程模板分页加载，分页参数[offset,limit]")
	@RequestMapping(value="/pages",method=RequestMethod.POST)
	public @ResponseBody MsgModel pages(@RequestBody @ApiParam(name="paramMap",value="分页参数[offset,limit]",required=true) Map<String, Object> paramMap)
	{
		CourseTemplateExample listExample=new CourseTemplateExample();
		PageModel<CourseTemplate> pageModel=null;
		if(Optional.of(paramMap)
		.filter(offset->paramMap.containsKey("offset")!=false)
		.filter(limit->paramMap.containsKey("limit")!=false)
		.isPresent())
		{
			pageModel=courseTemplateService.selectByExampleForOffsetPage(listExample,
					Integer.valueOf(paramMap.get("offset").toString()), Integer.valueOf(paramMap.get("limit").toString()));
		}
		return this.resultMsg(!this.isNull(pageModel)?HttpStatus.OK.value():HttpStatus.EXPECTATION_FAILED.value(),
				!this.isNull(pageModel)?"课程加载成功":"课程加载失败",!this.isNull(pageModel)?pageModel:"");
	}
}
